//
//  JXHomePhotoView.m
//  JXAddressApp
//
//  Created by zhhw_home on 2018/11/4.
//  Copyright © 2018年 zhhw_home. All rights reserved.
//

#import "JXHomePhotoView.h"
#import "QMUIButton.h"
#import "QMUICommonDefines.h"
#import "UIColor+QMUI.h"
#import <AFNetworking/UIButton+AFNetworking.h>
#import "NSArray+Safe.h"

@interface JXHomePhotoView ()

@property (nonatomic, strong)   UIButton    *button1;
@property (nonatomic, strong)   UIButton    *button2;
@property (nonatomic, strong)   UIButton    *button3;
@property (nonatomic, strong)   UIButton    *button4;

@end

@implementation JXHomePhotoView

- (instancetype)initWithFrame:(CGRect)frame
{
    CGFloat width = (SCREEN_WIDTH-50)/4.f;
    frame.size.height = width+40;
    
    if (self = [super initWithFrame:frame]) {
        [self initRootView];
    }
    
    return self;
}

- (void)initRootView
{
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, SCREEN_WIDTH, 20)];
    titleLabel.text = @"图片";
    titleLabel.textColor = [UIColor qmui_colorWithHexString:@"#333333"];
    titleLabel.font = [UIFont systemFontOfSize:14];
    titleLabel.textAlignment = NSTextAlignmentLeft;
    [self addSubview:titleLabel];
    
    CGFloat width = (SCREEN_WIDTH-50)/4.f;
    
    _button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    _button1.frame = CGRectMake(10, 30, width, width);
    [self addSubview:_button1];
    
    _button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    _button2.frame = CGRectMake(width+20, 30, width, width);
    [self addSubview:_button2];
    
    _button3 = [UIButton buttonWithType:UIButtonTypeCustom];
    _button3.frame = CGRectMake(2*width+30, 30, width, width);
    [self addSubview:_button3];
    
    _button4 = [UIButton buttonWithType:UIButtonTypeCustom];
    _button4.frame = CGRectMake(3*width+40, 30, width, width);
    [self addSubview:_button4];
    
    _button1.tag = 1;
    _button2.tag = 2;
    _button3.tag = 3;
    _button4.tag = 4;
    
    [_button1 addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
    [_button2 addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
    [_button3 addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
    [_button4 addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)setImages:(NSArray*)images
{
    NSDictionary *item = [images objectAtIndexSafe:0];
    if (item) {
        [_button1 setImageForState:UIControlStateNormal withURL:[NSURL URLWithString:[JXHomePhotoView imageUrl:item[@"url"]]]];
    }
    
    item = [images objectAtIndexSafe:1];
    if (item) {
        [_button2 setImageForState:UIControlStateNormal withURL:[NSURL URLWithString:[JXHomePhotoView imageUrl:item[@"url"]]]];
    }
    
    item = [images objectAtIndexSafe:2];
    if (item) {
        [_button3 setImageForState:UIControlStateNormal withURL:[NSURL URLWithString:[JXHomePhotoView imageUrl:item[@"url"]]]];
    }
    
    item = [images objectAtIndexSafe:3];
    if (item) {
        [_button4 setImageForState:UIControlStateNormal withURL:[NSURL URLWithString:[JXHomePhotoView imageUrl:item[@"url"]]]];
    }
}

+ (NSString*)imageUrl:(NSString*)image
{
    NSString *url = [[NSString alloc] initWithFormat:@"http://www.wegis.net/media%@", image];
    return [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];;
}

- (void)action:(UIButton*)sender
{
    if ([_delegate respondsToSelector:@selector(imageClick:view:)]) {
        [_delegate imageClick:sender.tag-1 view:sender];
    }
}

@end
